package com.kenyi.co.ui.home.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.Util;
import com.kenyi.co.GlideApp;
import com.kenyi.co.R;
import com.kenyi.co.common.utils.RouteUtils;
import com.kenyi.co.ui.home.bean.IndexListBean;
import com.kenyi.co.utils.BaseMultiAdapter;
import com.kenyi.co.utils.CommonUtils;
import com.kenyi.co.utils.GlideImageLoader;
import com.kenyi.co.utils.GlideRoundedCorners;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.SuperViewHolder;
import com.stay4it.banner.Banner;
import com.stay4it.banner.BannerConfig;
import com.stay4it.banner.Transformer;
import com.stay4it.banner.listener.OnBannerListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/29.
 */

public class HomeMainAdapter extends BaseMultiAdapter<IndexListBean.DatasBean> {


    public HomeMainAdapter(Context context) {
        super(context);

        addItemType(1, R.layout.fragment_right_item_2);
        addItemType(2, R.layout.video_list_ad_1);
        addItemType(3, R.layout.video_list_ad_2);
        addItemType(4, R.layout.video_list_ad_3);
        addItemType(5, R.layout.video_list_ad_4);


    }


    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        IndexListBean.DatasBean item = mDataList.get(position);
        int ItemType = 1;

        switch (item.getItemType()) {
            case 1:
                bindTextItem1(holder, item);
                break;
            case 2:
                bindTextItem2(holder, item);
                break;
            case 3:
                bindTextItem3(holder, item);
                break;
            case 4:
                bindTextItem4(holder, item);
                break;
            case 5:
                bindTextItem5(holder, item);
                break;
            default:
                break;
        }


    }

    private void bindTextItem1(SuperViewHolder holder, final IndexListBean.DatasBean item) {
        final ImageView iv_item_vodeo = holder.getView(R.id.iv_item_vodeo);
        TextView tv_video_right_title = holder.getView(R.id.tv_video_right_title);
        TextView tv_video_right_sub_title = holder.getView(R.id.tv_video_right_sub_title);
        TextView tv_video_right_num = holder.getView(R.id.tv_video_right_num);
        TextView tv_video_right_time = holder.getView(R.id.tv_video_right_time);
//        LinearLayout li_home_content = holder.getView(R.id.li_home_content);
//        li_home_content.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent metags = new Intent(mContext, VideoDetailActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("id", item.getId() + "");
//                metags.putExtras(bundle);
//                mContext.startActivity(metags);
//            }
//        });
        tv_video_right_time.setText(item.getVideoTime());
        tv_video_right_title.setText(item.getTitle());
        tv_video_right_sub_title.setText(item.getDesc());
        tv_video_right_num.setText("播放次数：" + item.getReadCount() + "次");

        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getVideoImg())
                        .placeholder(R.drawable.iv_bg_video_right)
                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_vodeo);
            }

        } catch (Exception e) {

        }
    }

    private void bindTextItem2(SuperViewHolder holder, IndexListBean.DatasBean item) {

        TextView tv_ad_1_title = holder.getView(R.id.tv_ad_1_title);
        ImageView iv_ad_1_1 = holder.getView(R.id.iv_ad_1_1);
        ImageView iv_ad_1_2 = holder.getView(R.id.iv_ad_1_2);
        ImageView iv_ad_1_3 = holder.getView(R.id.iv_ad_1_3);
        tv_ad_1_title.setText(item.getAds().get(0).getTitle());

        RequestOptions myOptions = new RequestOptions()
                .error(R.drawable.iv_ad_1_1)
                .placeholder(R.drawable.iv_ad_1_1)
                .fitCenter()
                .optionalTransform
                        (new GlideRoundedCorners(CommonUtils.dip2px(mContext, 3)
                                , GlideRoundedCorners.CornerType.LEFT));

        try {
            if (Util.isOnMainThread() && mContext != null) {
                Glide.with(mContext)
                        .load(item.getAds().get(0).getImgs().get(0))
                        .apply(myOptions)
                        .into(iv_ad_1_1);
            }

        } catch (Exception e) {

        }
        RequestOptions myOptions2 = new RequestOptions()
                .error(R.drawable.iv_ad_1_2)
                .placeholder(R.drawable.iv_ad_1_2)
                .fitCenter()
                .optionalTransform
                        (new GlideRoundedCorners(CommonUtils.dip2px(mContext, 0)
                                , GlideRoundedCorners.CornerType.LEFT));

        try {
            if (Util.isOnMainThread() && mContext != null) {
                Glide.with(mContext)
                        .load(item.getAds().get(0).getImgs().get(1))
                        .apply(myOptions2)
                        .into(iv_ad_1_2);
            }

        } catch (Exception e) {

        }

        RequestOptions myOptions3 = new RequestOptions()
                .placeholder(R.drawable.iv_ad_1_3)
                .error(R.drawable.iv_ad_1_3)
                .fitCenter()
                .optionalTransform
                        (new GlideRoundedCorners(CommonUtils.dip2px(mContext, 3)
                                , GlideRoundedCorners.CornerType.RIGHT));

        try {
            if (Util.isOnMainThread() && mContext != null) {
                Glide.with(mContext)
                        .load(item.getAds().get(0).getImgs().get(2))
                        .apply(myOptions3)
                        .into(iv_ad_1_3);
            }

        } catch (Exception e) {

        }


    }

    private void bindTextItem3(SuperViewHolder holder, IndexListBean.DatasBean item) {
        ImageView iv_item_vodeo = holder.getView(R.id.iv_item_vodeo);
        TextView tv_video_right_title = holder.getView(R.id.tv_video_right_title);
        tv_video_right_title.setText(item.getAds().get(0).getTitle());


        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getAds().get(0).getImgs().get(0))
                        .placeholder(R.drawable.iv_bg_video_right)

                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_vodeo);
            }

        } catch (Exception e) {

        }
    }

    private void bindTextItem4(SuperViewHolder holder, IndexListBean.DatasBean item) {
        ImageView iv_item_like = holder.getView(R.id.iv_item_like);
        TextView tv_video_title = holder.getView(R.id.tv_video_title);
        tv_video_title.setText(item.getAds().get(0).getTitle());


        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getAds().get(0).getImgs().get(0))
                        .placeholder(R.drawable.iv_bg_video_right)
                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_like);
            }

        } catch (Exception e) {

        }

    }

    private void bindTextItem5(SuperViewHolder holder, final IndexListBean.DatasBean item) {
        RelativeLayout li_home_vier = holder.getView(R.id.li_home_vier);
        TextView tv_video_title = holder.getView(R.id.tv_video_title);
        tv_video_title.setText(item.getAds().get(0).getTitle());
        ViewGroup.LayoutParams vParams = li_home_vier.getLayoutParams();
        float density = mContext.getResources().getDisplayMetrics().density;//dp转px
        int biankaung = (int) (32 * density + 0.5);
        vParams.height = (int) ((CommonUtils.getMobileWidth(mContext) - biankaung) * 0.47);
        vParams.width = (int) (CommonUtils.getMobileWidth(mContext) - biankaung);
        li_home_vier.setLayoutParams(vParams);
        Banner lunbo = holder.getView(R.id.banner);
        lunbo.setDelayTime(3000)
                .setBannerAnimation(Transformer.DepthPage)
                .setImages(item.getAds())
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setIndicatorGravity(BannerConfig.CENTER)
                .setImageLoader(new GlideImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        IndexListBean.DatasBean.AdsBean bannerInfo = (IndexListBean.DatasBean.AdsBean) path;
                        try {
                            if (Util.isOnMainThread() && context != null) {
                                RequestOptions myOptions = new RequestOptions()
                                        .error(R.drawable.iv_bg_video_right)
                                        .fitCenter()
                                        .optionalTransform
                                                (new GlideRoundedCorners(CommonUtils.dip2px(context, 5)
                                                        , GlideRoundedCorners.CornerType.ALL));
                                Glide.with(context)
                                        .load(bannerInfo.getImgs().get(0))
                                        .apply(myOptions)
                                        .into(imageView);
                            }

                        } catch (Exception e) {

                        }

                    }
                }).start();

        lunbo.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {


                Map<String, String> params = new HashMap<>();


                params.put("title", "");
                params.put("articleId", "");
                params.put("url2", item.getAds().get(0).getUrl2());
                params.put("advid", item.getAds().get(0).getId());
                params.put("adPosition", "0");
                params.put("pageName", CommonUtils.getTopActivity(mContext));
                RouteUtils.RoutebyAd(mContext.getApplicationContext(), mContext, item.getAds().get(0).getUrl(), params);


            }
        });
    }


}
